package com.funo.wcity.app.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.funo.wcity.app.entity.TblMailDetail;
import com.funo.wcity.app.utils.StringUtil;

@Repository("tblMailDetailDAO")
public class TblMailDetailDAO extends DefaultBaseDao<TblMailDetail,Long>{

	/**
	 * 查询邮件明细并分页
	 * @param 
	 * 		pageNo
	 * 			当前页数
	 * @param 
	 * 		pageSize
	 * 			每页的记录数
	 * @param 
	 * 		tblMailDetail
	 * 			
	 * @return
	 */
	public Page getTbLMailDetailList(Integer pageNo, Integer pageSize,
			TblMailDetail tblMailDetail,String queryStr) {
		StringBuffer sql = new StringBuffer(
				"select m.idMailDetail,m.messageUID,m.idUser,m.idCompanyType,m.idCompany,m.idContactPerson,m.CopySend,m.SecretSend,m.RecPeople,m.Content "
						+ ",m.SendPeople,(select count(*) from tblMailAnnex an where an.idMailAnnex in(m.idMailAnnex))idMailAnnex,m.state, "
						+ "m.mailSign,m.Title,m.shortContent,m.ipCountry,m.ipCompany,date_format(m.lastDate,'%Y-%c-%d') lastDate "
						+ "from tblMailDetail m "
						+ " where 1=1");
	    if(StringUtil.isEmpty(queryStr)){
			if(!StringUtil.isEmpty(tblMailDetail.getState())) {  //邮箱状态
				sql.append(" and m.State=").append(tblMailDetail.getState());
			}
	    }else{
	    	sql.append(" and m.State not in(4,6)");
	    }
		if(!StringUtil.isEmpty(tblMailDetail.getIdUser())){  //当前用户
			sql.append(" and m.idUser=").append(tblMailDetail.getIdUser());
		}
		if(!StringUtil.isEmpty(tblMailDetail.getIdCompanyType())){ //公司类型
			if("0".equals(tblMailDetail.getIdCompanyType())){
				sql.append(" and m.idContactPerson is null and m.idCompanyType is null and m.idCompany is null");
			}else{
				sql.append(" and m.idCompanyType like '%,").append(tblMailDetail.getIdCompanyType()).append(",%'");
			}
		}
		if(!StringUtil.isEmpty(tblMailDetail.getIdCompany())){ //公司id
			sql.append(" and m.idCompany like '%,").append(tblMailDetail.getIdCompany()).append(",%'");
		}
		if(!StringUtil.isEmpty(tblMailDetail.getIdContactPerson())){//联系人
			sql.append(" and m.idContactPerson like '%,").append(tblMailDetail.getIdContactPerson()).append(",%'");
		}
		if(!StringUtil.isEmpty(queryStr)){//模糊查询标题、内容、收件人、发件人
			sql.append(" and ( m.Title like '%").append(queryStr).append("%'")
			.append(" or m.Content like '%").append(queryStr).append("%'")
			.append(" or m.RecPeople like '%").append(queryStr).append("%'")
			.append(" or m.SendPeople like '%").append(queryStr).append("%' )");
		}
		sql.append(" order by  m.lastDate desc");
		return this.getPageBySQL(pageNo, pageSize, sql.toString());
	}
	/**
	 * 获取邮件明细所有MessageUid
	 */
	public String getMessageUid(Integer idUser){
		String sql="select messageUID  from tblMailDetail m where 1=1 and m.idUser="+idUser;
		List messageUidList=this.jdbcTemplate.queryForList(sql);
		StringBuffer messageUidStr=new StringBuffer();
		if(messageUidList.size()>0){
			messageUidStr.append(",");
			for(int i=0;i<messageUidList.size();i++){
				Map Obj=(Map)messageUidList.get(i);
				messageUidStr.append(Obj.get("MESSAGEUID")).append(",");
			}
		}
		return messageUidStr.toString();
	}
	
	/**
	 * 多个主键ID，修改邮箱信息
	 */
	public void updateTbLMailDetail(TblMailDetail tblMailDetail,String idMailDetail){
		StringBuffer sql=new StringBuffer("update tblMailDetail m set ");
		if(!StringUtil.isEmpty(tblMailDetail.getMailSign())){ //1已读标志，0未读标志
			sql.append(" m.mailSign=").append(tblMailDetail.getMailSign());
		}
		if(!StringUtil.isEmpty(tblMailDetail.getState())){// 邮件状态 0：正常，接收邮件  1：收件夹  2:草稿箱 3:已发送 4:已删除 5:垃圾邮件 6:彻底删除
			sql.append(" m.state=").append(tblMailDetail.getState());
		}
		sql.append("  where 1=1 and m.idMailDetail in(").append(idMailDetail).append(")");
		this.jdbcTemplate.update(sql.toString());
	}
	
}
